home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /***************************************************************************
- * @(#) - BZ - Multiplayer tank game - Framerate Control.
- *
- * $Id: framerate.c,v 1.4 1993/08/11 19:46:56 adele Exp $
- *
- * Chris Fouts - Silicon Graphics, Inc.
- * October, 1991
- **************************************************************************/
- #include <sys/time.h>
- #include <bstring.h>
- #include "framerate.h"
-
- #define DEFAULT_FRAME_RATE 10
-
- static char *version_id = "$Id: framerate.c,v 1.4 1993/08/11 19:46:56 adele Exp $" ;
-
- static int frame_rate = DEFAULT_FRAME_RATE ;
- static float frame_time = 1.f / DEFAULT_FRAME_RATE ;
- static struct timeval frame_start, frame_end ;
-
-
-
- void set_frame_rate(
- int new_rate
- )
- {
- if( new_rate > 0 ) {
- frame_rate = new_rate ;
- frame_time = 1.f / frame_rate ;
- }
-
- start_time( &frame_start ) ;
- }
-
-
-
- void copy_time(
- struct timeval *dst,
- struct timeval *src
- )
- {
- bcopy( src, dst, sizeof( struct timeval ) ) ;
- }
-
-
-
- void set_time(
- struct timeval *dst,
- struct timeval *src,
- long secs,
- long usecs
- )
- {
- copy_time( dst, src ) ;
- add_to_time( dst, secs, usecs ) ;
- }
-
-
- void add_to_time(
- struct timeval *dst,
- long secs,
- long usecs
- )
- {
- dst->tv_usec += usecs ;
- dst->tv_sec += secs + ( dst->tv_usec / 1000000 ) ;
- dst->tv_usec %= 1000000 ;
- }
-
-
-
- float pace_frame(
- struct timeval *buf
- )
- {
- float elapsed_time ;
-
- do {
- elapsed_time = end_time( &frame_start, &frame_end ) ;
- } while( elapsed_time < frame_time ) ;
- start_time( &frame_start ) ;
-
- if( buf != NULL ) {
- copy_time( buf, &frame_start ) ;
- }
-
- return( elapsed_time ) ;
- }
-
-
-
- void start_time(
- struct timeval *buf
- )
- {
- struct timezone tzp ;
-
- gettimeofday( buf, &tzp ) ;
- }
-
-
-
- float end_time(
- struct timeval *bufThen,
- struct timeval *bufNow
- )
- {
- long secs ;
- long msecs ;
- struct timeval tv ;
- struct timeval *now ;
- struct timeval *then = (struct timeval *)bufThen ;
- struct timezone tzp ;
-
- if( bufNow != NULL )
- now = (struct timeval *)bufNow ;
- else
- now = &tv ;
-
- gettimeofday( now, &tzp ) ;
-
- return( (float)( ( now->tv_sec - then->tv_sec ) +
- ( now->tv_usec - then->tv_usec ) / 1000000.f ) ) ;
- }
-
-
-
- long elapsed_sec_tenths(
- struct timeval *bufThen,
- struct timeval *bufNow
- )
- {
- struct timeval tv ;
- struct timeval *now ;
- struct timeval *then = bufThen ;
- struct timezone tzp ;
-
- if( bufNow != NULL ) {
- now = bufNow ;
- }
- else {
- now = &tv ;
- gettimeofday( now, &tzp ) ;
- }
-
-
- return( ( ( now->tv_sec - then->tv_sec ) * 10 +
- ( now->tv_usec - then->tv_usec ) / 100000 ) ) ;
- }
-
-
-
-